home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML ConsoleMax.sea / XML ConsoleMax / Required / swingall.jar / javax / swing / plaf / basic / BasicToolBarUI.class (.txt) < prev    next >
Encoding:
Java Class File  |  1999-07-15  |  12.5 KB  |  558 lines

  1. package javax.swing.plaf.basic;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Component;
  5. import java.awt.Container;
  6. import java.awt.Dimension;
  7. import java.awt.Frame;
  8. import java.awt.IllegalComponentStateException;
  9. import java.awt.Point;
  10. import java.awt.Window;
  11. import java.awt.event.ContainerListener;
  12. import java.awt.event.FocusListener;
  13. import java.awt.event.WindowListener;
  14. import java.beans.PropertyChangeListener;
  15. import javax.swing.JComponent;
  16. import javax.swing.JFrame;
  17. import javax.swing.JToolBar;
  18. import javax.swing.KeyStroke;
  19. import javax.swing.LookAndFeel;
  20. import javax.swing.SwingConstants;
  21. import javax.swing.UIManager;
  22. import javax.swing.event.MouseInputListener;
  23. import javax.swing.plaf.ComponentUI;
  24. import javax.swing.plaf.ToolBarUI;
  25. import javax.swing.plaf.UIResource;
  26.  
  27. public class BasicToolBarUI extends ToolBarUI implements SwingConstants {
  28.    protected JToolBar toolBar;
  29.    private boolean floating;
  30.    private int floatingX;
  31.    private int floatingY;
  32.    private JFrame floatingFrame;
  33.    protected DragWindow dragWindow;
  34.    private Container dockingSource;
  35.    private int dockingSensitivity = 0;
  36.    protected int focusedCompIndex = -1;
  37.    protected Color dockingColor = null;
  38.    protected Color floatingColor = null;
  39.    protected Color dockingBorderColor = null;
  40.    protected Color floatingBorderColor = null;
  41.    protected MouseInputListener dockingListener;
  42.    protected PropertyChangeListener propertyListener;
  43.    protected ContainerListener toolBarContListener;
  44.    protected FocusListener toolBarFocusListener;
  45.    protected KeyStroke upKey;
  46.    protected KeyStroke downKey;
  47.    protected KeyStroke leftKey;
  48.    protected KeyStroke rightKey;
  49.    private KeyStroke kpUpKey;
  50.    private KeyStroke kpDownKey;
  51.    private KeyStroke kpLeftKey;
  52.    private KeyStroke kpRightKey;
  53.    private static String FOCUSED_COMP_INDEX = "JToolBar.focusedCompIndex";
  54.  
  55.    public boolean canDock(Component var1, Point var2) {
  56.       boolean var3 = false;
  57.       if (var1.contains(var2)) {
  58.          if (this.dockingSensitivity == 0) {
  59.             this.dockingSensitivity = this.toolBar.getSize().height;
  60.          }
  61.  
  62.          if (var2.y < this.dockingSensitivity) {
  63.             var3 = true;
  64.          }
  65.  
  66.          if (var2.y > var1.getSize().height - this.dockingSensitivity) {
  67.             var3 = true;
  68.          }
  69.  
  70.          if (var2.x < this.dockingSensitivity) {
  71.             var3 = true;
  72.          }
  73.  
  74.          if (var2.x > var1.getSize().width - this.dockingSensitivity) {
  75.             var3 = true;
  76.          }
  77.       }
  78.  
  79.       return var3;
  80.    }
  81.  
  82.    protected MouseInputListener createDockingListener() {
  83.       return new DockingListener(this, this.toolBar);
  84.    }
  85.  
  86.    protected DragWindow createDragWindow(JToolBar var1) {
  87.       Object var2 = null;
  88.       if (this.toolBar != null) {
  89.          Container var3;
  90.          for(var3 = this.toolBar.getParent(); var3 != null && !(var3 instanceof Frame); var3 = ((Component)var3).getParent()) {
  91.          }
  92.  
  93.          if (var3 != null && var3 instanceof Frame) {
  94.             Frame var4 = (Frame)var3;
  95.          }
  96.       }
  97.  
  98.       if (this.floatingFrame == null) {
  99.          this.floatingFrame = this.createFloatingFrame(this.toolBar);
  100.       }
  101.  
  102.       JFrame var5 = this.floatingFrame;
  103.       DragWindow var6 = new DragWindow(this, var5);
  104.       return var6;
  105.    }
  106.  
  107.    protected JFrame createFloatingFrame(JToolBar var1) {
  108.       JFrame var2 = new JFrame(((Component)var1).getName());
  109.       ((Frame)var2).setResizable(false);
  110.       WindowListener var3 = this.createFrameListener();
  111.       ((Window)var2).addWindowListener(var3);
  112.       return var2;
  113.    }
  114.  
  115.    protected WindowListener createFrameListener() {
  116.       return new FrameListener(this);
  117.    }
  118.  
  119.    protected PropertyChangeListener createPropertyListener() {
  120.       return new PropertyListener(this);
  121.    }
  122.  
  123.    protected ContainerListener createToolBarContListener() {
  124.       return new ToolBarContListener(this);
  125.    }
  126.  
  127.    protected FocusListener createToolBarFocusListener() {
  128.       return new ToolBarFocusListener(this);
  129.    }
  130.  
  131.    public static ComponentUI createUI(JComponent var0) {
  132.       return new BasicToolBarUI();
  133.    }
  134.  
  135.    protected void dragTo(Point var1, Point var2) {
  136.       if (this.toolBar.isFloatable()) {
  137.          try {
  138.             if (this.dragWindow == null) {
  139.                this.dragWindow = this.createDragWindow(this.toolBar);
  140.             }
  141.  
  142.             Point var3 = this.dragWindow.getOffset();
  143.             if (var3 == null) {
  144.                Dimension var4 = this.toolBar.getPreferredSize();
  145.                var3 = new Point(var4.width / 2, var4.height / 2);
  146.                this.dragWindow.setOffset(var3);
  147.             }
  148.  
  149.             Point var11 = new Point(var2.x + var1.x, var2.y + var1.y);
  150.             Point var5 = new Point(var11.x - var3.x, var11.y - var3.y);
  151.             if (this.dockingSource == null) {
  152.                this.dockingSource = this.toolBar.getParent();
  153.             }
  154.  
  155.             Point var6 = this.dockingSource.getLocationOnScreen();
  156.             Point var7 = new Point(var11.x - var6.x, var11.y - var6.y);
  157.             if (this.canDock(this.dockingSource, var7)) {
  158.                this.dragWindow.setBackground(this.getDockingColor());
  159.                String var8 = this.getDockingConstraint(this.dockingSource, var7);
  160.                int var9 = this.mapConstraintToOrientation(var8);
  161.                this.dragWindow.setOrientation(var9);
  162.                this.dragWindow.setBorderColor(this.dockingBorderColor);
  163.             } else {
  164.                this.dragWindow.setBackground(this.getFloatingColor());
  165.                this.dragWindow.setOrientation(0);
  166.                this.dragWindow.setBorderColor(this.floatingBorderColor);
  167.             }
  168.  
  169.             this.dragWindow.setLocation(var5.x, var5.y);
  170.             if (!this.dragWindow.isVisible()) {
  171.                Dimension var12 = this.toolBar.getPreferredSize();
  172.                this.dragWindow.setSize(var12.width, var12.height);
  173.                this.dragWindow.show();
  174.             }
  175.          } catch (IllegalComponentStateException var10) {
  176.          }
  177.       }
  178.  
  179.    }
  180.  
  181.    protected void floatAt(Point var1, Point var2) {
  182.       if (this.toolBar.isFloatable()) {
  183.          try {
  184.             Point var3 = this.dragWindow.getOffset();
  185.             if (var3 == null) {
  186.                var3 = var1;
  187.                this.dragWindow.setOffset(var1);
  188.             }
  189.  
  190.             Point var4 = new Point(var2.x + var1.x, var2.y + var1.y);
  191.             this.setFloatingLocation(var4.x - var3.x, var4.y - var3.y);
  192.             if (this.dockingSource != null) {
  193.                Point var5 = this.dockingSource.getLocationOnScreen();
  194.                Point var6 = new Point(var4.x - var5.x, var4.y - var5.y);
  195.                if (this.canDock(this.dockingSource, var6)) {
  196.                   this.setFloating(false, var6);
  197.                } else {
  198.                   this.setFloating(true, (Point)null);
  199.                }
  200.             } else {
  201.                this.setFloating(true, (Point)null);
  202.             }
  203.  
  204.             this.dragWindow.setOffset((Point)null);
  205.          } catch (IllegalComponentStateException var7) {
  206.          }
  207.       }
  208.  
  209.    }
  210.  
  211.    public Color getDockingColor() {
  212.       return this.dockingColor;
  213.    }
  214.  
  215.    private String getDockingConstraint(Component var1, Point var2) {
  216.       String var3 = "North";
  217.       if (var2 != null && var1.contains(var2)) {
  218.          if (this.dockingSensitivity == 0) {
  219.             this.dockingSensitivity = this.toolBar.getSize().height;
  220.          }
  221.  
  222.          if (var2.y > var1.getSize().height - this.dockingSensitivity) {
  223.             var3 = "South";
  224.          }
  225.  
  226.          if (var2.x < this.dockingSensitivity) {
  227.             var3 = "West";
  228.          }
  229.  
  230.          if (var2.x > var1.getSize().width - this.dockingSensitivity) {
  231.             var3 = "East";
  232.          }
  233.  
  234.          if (var2.y < this.dockingSensitivity) {
  235.             var3 = "North";
  236.          }
  237.       }
  238.  
  239.       return var3;
  240.    }
  241.  
  242.    public Color getFloatingColor() {
  243.       return this.floatingColor;
  244.    }
  245.  
  246.    public Dimension getMaximumSize(JComponent var1) {
  247.       return this.getPreferredSize(var1);
  248.    }
  249.  
  250.    public Dimension getMinimumSize(JComponent var1) {
  251.       return this.getPreferredSize(var1);
  252.    }
  253.  
  254.    public Dimension getPreferredSize(JComponent var1) {
  255.       return null;
  256.    }
  257.  
  258.    protected void installComponents() {
  259.    }
  260.  
  261.    protected void installDefaults() {
  262.       LookAndFeel.installBorder(this.toolBar, "ToolBar.border");
  263.       LookAndFeel.installColorsAndFont(this.toolBar, "ToolBar.background", "ToolBar.foreground", "ToolBar.font");
  264.       if (this.dockingColor == null || this.dockingColor instanceof UIResource) {
  265.          this.dockingColor = UIManager.getColor("ToolBar.dockingBackground");
  266.       }
  267.  
  268.       if (this.floatingColor == null || this.floatingColor instanceof UIResource) {
  269.          this.floatingColor = UIManager.getColor("ToolBar.floatingBackground");
  270.       }
  271.  
  272.       if (this.dockingBorderColor == null || this.dockingBorderColor instanceof UIResource) {
  273.          this.dockingBorderColor = UIManager.getColor("ToolBar.dockingForeground");
  274.       }
  275.  
  276.       if (this.floatingBorderColor == null || this.floatingBorderColor instanceof UIResource) {
  277.          this.floatingBorderColor = UIManager.getColor("ToolBar.floatingForeground");
  278.       }
  279.  
  280.    }
  281.  
  282.    protected void installKeyboardActions() {
  283.       UpAction var1 = new UpAction(this);
  284.       DownAction var2 = new DownAction(this);
  285.       LeftAction var3 = new LeftAction(this);
  286.       RightAction var4 = new RightAction(this);
  287.       this.upKey = KeyStroke.getKeyStroke(38, 0);
  288.       this.downKey = KeyStroke.getKeyStroke(40, 0);
  289.       this.leftKey = KeyStroke.getKeyStroke(37, 0);
  290.       this.rightKey = KeyStroke.getKeyStroke(39, 0);
  291.       this.kpUpKey = KeyStroke.getKeyStroke("KP_UP");
  292.       this.kpDownKey = KeyStroke.getKeyStroke("KP_DOWN");
  293.       this.kpLeftKey = KeyStroke.getKeyStroke("KP_LEFT");
  294.       this.kpRightKey = KeyStroke.getKeyStroke("KP_RIGHT");
  295.       this.toolBar.registerKeyboardAction(var1, this.upKey, 1);
  296.       this.toolBar.registerKeyboardAction(var2, this.downKey, 1);
  297.       this.toolBar.registerKeyboardAction(var3, this.leftKey, 1);
  298.       this.toolBar.registerKeyboardAction(var4, this.rightKey, 1);
  299.       this.toolBar.registerKeyboardAction(var1, this.kpUpKey, 1);
  300.       this.toolBar.registerKeyboardAction(var2, this.kpDownKey, 1);
  301.       this.toolBar.registerKeyboardAction(var3, this.kpLeftKey, 1);
  302.       this.toolBar.registerKeyboardAction(var4, this.kpRightKey, 1);
  303.    }
  304.  
  305.    protected void installListeners() {
  306.       this.dockingListener = this.createDockingListener();
  307.       if (this.dockingListener != null) {
  308.          this.toolBar.addMouseMotionListener(this.dockingListener);
  309.          this.toolBar.addMouseListener(this.dockingListener);
  310.       }
  311.  
  312.       this.propertyListener = this.createPropertyListener();
  313.       this.toolBarContListener = this.createToolBarContListener();
  314.       if (this.toolBarContListener != null) {
  315.          this.toolBar.addContainerListener(this.toolBarContListener);
  316.       }
  317.  
  318.       this.toolBarFocusListener = this.createToolBarFocusListener();
  319.       if (this.toolBarFocusListener != null) {
  320.          Component[] var1 = this.toolBar.getComponents();
  321.  
  322.          for(int var2 = 0; var2 < var1.length; ++var2) {
  323.             var1[var2].addFocusListener(this.toolBarFocusListener);
  324.          }
  325.       }
  326.  
  327.    }
  328.  
  329.    public void installUI(JComponent var1) {
  330.       this.toolBar = (JToolBar)var1;
  331.       this.installDefaults();
  332.       this.installComponents();
  333.       this.installListeners();
  334.       this.installKeyboardActions();
  335.       this.dockingSensitivity = 0;
  336.       this.floating = false;
  337.       this.floatingX = this.floatingY = 0;
  338.       this.floatingFrame = null;
  339.       this.setOrientation(this.toolBar.getOrientation());
  340.       var1.setOpaque(true);
  341.       if (var1.getClientProperty(FOCUSED_COMP_INDEX) != null) {
  342.          this.focusedCompIndex = (Integer)var1.getClientProperty(FOCUSED_COMP_INDEX);
  343.       }
  344.  
  345.    }
  346.  
  347.    public boolean isFloating() {
  348.       return this.floating;
  349.    }
  350.  
  351.    private int mapConstraintToOrientation(String var1) {
  352.       int var2 = this.toolBar.getOrientation();
  353.       if (var1 != null) {
  354.          if (!var1.equals("East") && !var1.equals("West")) {
  355.             if (var1.equals("North") || var1.equals("South")) {
  356.                var2 = 0;
  357.             }
  358.          } else {
  359.             var2 = 1;
  360.          }
  361.       }
  362.  
  363.       return var2;
  364.    }
  365.  
  366.    protected void navigateFocusedComp(int var1) {
  367.       int var2 = this.toolBar.getComponentCount();
  368.       switch (var1) {
  369.          case 1:
  370.          case 7:
  371.             if (this.focusedCompIndex >= 0 && this.focusedCompIndex < var2) {
  372.                int var5 = this.focusedCompIndex - 1;
  373.  
  374.                while(var5 != this.focusedCompIndex) {
  375.                   if (var5 < 0) {
  376.                      var5 = var2 - 1;
  377.                   }
  378.  
  379.                   Component var6 = this.toolBar.getComponentAtIndex(var5--);
  380.                   if (var6 != null && var6.isFocusTraversable()) {
  381.                      var6.requestFocus();
  382.                      break;
  383.                   }
  384.                }
  385.             }
  386.          case 2:
  387.          case 4:
  388.          case 6:
  389.          default:
  390.             break;
  391.          case 3:
  392.          case 5:
  393.             if (this.focusedCompIndex >= 0 && this.focusedCompIndex < var2) {
  394.                int var3 = this.focusedCompIndex + 1;
  395.  
  396.                while(var3 != this.focusedCompIndex) {
  397.                   if (var3 >= var2) {
  398.                      var3 = 0;
  399.                   }
  400.  
  401.                   Component var4 = this.toolBar.getComponentAtIndex(var3++);
  402.                   if (var4 != null && var4.isFocusTraversable()) {
  403.                      var4.requestFocus();
  404.                      break;
  405.                   }
  406.                }
  407.             }
  408.       }
  409.  
  410.    }
  411.  
  412.    public void setDockingColor(Color var1) {
  413.       this.dockingColor = var1;
  414.    }
  415.  
  416.    public void setFloating(boolean var1, Point var2) {
  417.       if (this.toolBar.isFloatable()) {
  418.          if (this.dragWindow != null) {
  419.             this.dragWindow.setVisible(false);
  420.          }
  421.  
  422.          this.floating = var1;
  423.          if (var1) {
  424.             if (this.dockingSource == null) {
  425.                this.dockingSource = this.toolBar.getParent();
  426.                this.dockingSource.remove(this.toolBar);
  427.             }
  428.  
  429.             if (this.propertyListener != null) {
  430.                UIManager.addPropertyChangeListener(this.propertyListener);
  431.             }
  432.  
  433.             if (this.floatingFrame == null) {
  434.                this.floatingFrame = this.createFloatingFrame(this.toolBar);
  435.             }
  436.  
  437.             this.floatingFrame.getContentPane().add(this.toolBar, "Center");
  438.             this.setOrientation(0);
  439.             this.floatingFrame.pack();
  440.             this.floatingFrame.setLocation(this.floatingX, this.floatingY);
  441.             this.floatingFrame.show();
  442.          } else {
  443.             if (this.floatingFrame == null) {
  444.                this.floatingFrame = this.createFloatingFrame(this.toolBar);
  445.             }
  446.  
  447.             this.floatingFrame.setVisible(false);
  448.             this.floatingFrame.getContentPane().remove(this.toolBar);
  449.             String var3 = this.getDockingConstraint(this.dockingSource, var2);
  450.             int var4 = this.mapConstraintToOrientation(var3);
  451.             this.setOrientation(var4);
  452.             if (this.dockingSource == null) {
  453.                this.dockingSource = this.toolBar.getParent();
  454.             }
  455.  
  456.             if (this.propertyListener != null) {
  457.                UIManager.removePropertyChangeListener(this.propertyListener);
  458.             }
  459.  
  460.             this.dockingSource.add(var3, this.toolBar);
  461.          }
  462.  
  463.          this.dockingSource.invalidate();
  464.          Container var5 = this.dockingSource.getParent();
  465.          if (var5 != null) {
  466.             var5.validate();
  467.          }
  468.  
  469.          this.dockingSource.repaint();
  470.       }
  471.  
  472.    }
  473.  
  474.    public void setFloatingColor(Color var1) {
  475.       this.floatingColor = var1;
  476.    }
  477.  
  478.    public void setFloatingLocation(int var1, int var2) {
  479.       this.floatingX = var1;
  480.       this.floatingY = var2;
  481.    }
  482.  
  483.    public void setOrientation(int var1) {
  484.       this.toolBar.setOrientation(var1);
  485.       if (this.dragWindow != null) {
  486.          this.dragWindow.setOrientation(var1);
  487.       }
  488.  
  489.    }
  490.  
  491.    protected void uninstallComponents() {
  492.    }
  493.  
  494.    protected void uninstallDefaults() {
  495.       LookAndFeel.uninstallBorder(this.toolBar);
  496.       this.dockingColor = null;
  497.       this.floatingColor = null;
  498.       this.dockingBorderColor = null;
  499.       this.floatingBorderColor = null;
  500.    }
  501.  
  502.    protected void uninstallKeyboardActions() {
  503.       this.toolBar.unregisterKeyboardAction(this.upKey);
  504.       this.toolBar.unregisterKeyboardAction(this.downKey);
  505.       this.toolBar.unregisterKeyboardAction(this.leftKey);
  506.       this.toolBar.unregisterKeyboardAction(this.rightKey);
  507.       this.upKey = this.downKey = this.rightKey = this.leftKey = null;
  508.       this.toolBar.unregisterKeyboardAction(this.kpUpKey);
  509.       this.toolBar.unregisterKeyboardAction(this.kpDownKey);
  510.       this.toolBar.unregisterKeyboardAction(this.kpLeftKey);
  511.       this.toolBar.unregisterKeyboardAction(this.kpRightKey);
  512.       this.kpUpKey = this.kpDownKey = this.kpRightKey = this.kpLeftKey = null;
  513.    }
  514.  
  515.    protected void uninstallListeners() {
  516.       if (this.dockingListener != null) {
  517.          this.toolBar.removeMouseMotionListener(this.dockingListener);
  518.          this.toolBar.removeMouseListener(this.dockingListener);
  519.          this.dockingListener = null;
  520.       }
  521.  
  522.       if (this.propertyListener != null) {
  523.          this.propertyListener = null;
  524.       }
  525.  
  526.       if (this.toolBarContListener != null) {
  527.          this.toolBar.removeContainerListener(this.toolBarContListener);
  528.          this.toolBarContListener = null;
  529.       }
  530.  
  531.       if (this.toolBarFocusListener != null) {
  532.          Component[] var1 = this.toolBar.getComponents();
  533.  
  534.          for(int var2 = 0; var2 < var1.length; ++var2) {
  535.             var1[var2].removeFocusListener(this.toolBarFocusListener);
  536.          }
  537.  
  538.          this.toolBarFocusListener = null;
  539.       }
  540.  
  541.    }
  542.  
  543.    public void uninstallUI(JComponent var1) {
  544.       this.uninstallDefaults();
  545.       this.uninstallComponents();
  546.       this.uninstallListeners();
  547.       this.uninstallKeyboardActions();
  548.       if (this.isFloating()) {
  549.          this.setFloating(false, (Point)null);
  550.       }
  551.  
  552.       this.floatingFrame = null;
  553.       this.dragWindow = null;
  554.       this.dockingSource = null;
  555.       var1.putClientProperty(FOCUSED_COMP_INDEX, new Integer(this.focusedCompIndex));
  556.    }
  557. }
  558.